<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="150px">
			<!-- <el-form-item label="转入供电单位编号" prop="transferOrgNo">
				<el-input v-model="queryParams.transferOrgNo" placeholder="请输入转入供电单位编号" clearable @keyup.enter="handleQuery" />
			</el-form-item> -->
			<el-form-item label="转存任务编号" prop="transferNo">
				<el-input v-model="queryParams.transferNo" placeholder="请输入转存任务编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
		<!-- 	<el-form-item label="转出档案室编号" prop="transferOutRoomNo">
				<el-input v-model="queryParams.transferOutRoomNo" placeholder="请输入转出档案室编号" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item label="转入档案室编号" prop="transferinRoomNo">
				<el-input v-model="queryParams.transferinRoomNo" placeholder="请输入转入档案室编号" clearable @keyup.enter="handleQuery" />
			</el-form-item> -->
			<el-form-item label="转存状态" prop="transferStatus">
				<el-select v-model="queryParams.transferStatus" placeholder="请选择转存状态" clearable>
					<el-option v-for="dict in rollover_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item label="出入库状态" prop="status">
				<el-select v-model="queryParams.status" placeholder="请选择出入库状态" clearable>
					<el-option v-for="dict in upload_in_out_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item  label="出库推送状态" prop="pushStatus">
				<el-select v-model="queryParams.pushStatus" placeholder="请选择出库推送状态" clearable>
					<el-option v-for="dict in push_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item   label="入库推送状态" prop="inPushStatus">
				<el-select v-model="queryParams.inPushStatus" placeholder="请选择入库推送状态" clearable>
					<el-option v-for="dict in push_status" :key="dict.value" :label="dict.label" :value="dict.value" />
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="primary"-->
			<!--          plain-->
			<!--          icon="Plus"-->
			<!--          @click="handleAdd"-->
			<!--          v-hasPermi="['archive:ArchiveUploadTask:add']"-->
			<!--        >新增</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="success"-->
			<!--          plain-->
			<!--          icon="Edit"-->
			<!--          :disabled="single"-->
			<!--          @click="handleUpdate"-->
			<!--          v-hasPermi="['archive:ArchiveUploadTask:edit']"-->
			<!--        >修改</el-button>-->
			<!--      </el-col>-->
			<!--      <el-col :span="1.5">-->
			<!--        <el-button-->
			<!--          type="danger"-->
			<!--          plain-->
			<!--          icon="Delete"-->
			<!--          :disabled="multiple"-->
			<!--          @click="handleDelete"-->
			<!--          v-hasPermi="['archive:ArchiveUploadTask:remove']"-->
			<!--        >删除</el-button>-->
			<!--      </el-col>-->
			<!-- <el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['archive:ArchiveUploadTask:export']">导出</el-button>
			</el-col> -->
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="ArchiveUploadTaskList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<el-table-column label="序号" align='center' width="50">
				<template #default="scope">
					{{ scope.$index+1 }}
				</template>
			</el-table-column>
			
			<el-table-column label="出入库状态" align="center" prop="status" width="150">
				<template #default="scope">
					<dict-tag :options="upload_in_out_status" :value="scope.row.status" width="100" />
				</template>
			</el-table-column>
			<el-table-column label="转存状态" align="center" prop="transferStatus">
				<template #default="scope">
					<dict-tag :options="rollover_status" :value="scope.row.transferStatus" width="100" />
				</template>
			</el-table-column>
			<el-table-column label="出库推送状态" align="center" prop="pushStatus" width="150">
				<template #default="scope">
					<dict-tag :options="push_status" :value="scope.row.pushStatus" width="100" />
				</template>
			</el-table-column>
			<el-table-column label="入库推送状态" align="center" prop="inPushStatus" width="150">
				<template #default="scope">
					<dict-tag :options="push_status" :value="scope.row.inPushStatus" width="100" />
				</template>
			</el-table-column>
			<el-table-column label="转存任务编号" align="center" prop="transferNo" width="200" />
			<!-- <el-table-column label="申请供电单位编号" align="center" prop="orgNo" width="200" /> -->
		<!-- 	<el-table-column label="转入供电单位编号" align="center" prop="transferOrgNo" width="150" />
			<el-table-column label="转出档案室编号" align="center" prop="transferOutRoomNo" width="150" />
			<el-table-column label="转入档案室编号" align="center" prop="transferInRoomNo" width="150" /> -->
			<el-table-column label="盒数量" align="center" prop="boxNum" width="80" />
			<el-table-column label="袋数量" align="center" prop="bagNum" width="80" />
			<el-table-column label="档案数量" align="center" prop="archNum" width="80" />
			<el-table-column label="申请原因" align="center" prop="appReason" width="200" />

			<el-table-column label="操作" align="center" fixed="right" width="270">
				<template #default="scope">
					<el-button type="text" icon="View" @click="handleUpdate(scope.row)" v-hasPermi="['archive:ArchiveUploadTask:edit']">详情&emsp;</el-button>
					<!-- <el-button v-if="scope.row.status == '1'" type="text" icon="folder-remove" @click="revamp(scope.row,1,scope.$index+1)">出库&emsp;</el-button> -->
					<el-link v-if="scope.row.transferStatus == '01' && scope.row.pushStatus=='02'" type="warning" icon="Upload" @click="report(scope.row,scope.$index+1,1)">出库上报&emsp;</el-link>
					<!-- <el-button v-if="scope.row.status == '2'" type="text" icon="folder-remove" @click="revamp(scope.row,2)">入库&emsp;</el-button> -->
					<el-link v-if="scope.row.transferStatus == '02' && scope.row.inPushStatus=='02'" type="warning" icon="Upload" @click="report(scope.row,scope.$index+1,2)">入库上报</el-link>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 查看详情 -->
		<el-dialog :title="title" v-model="open" width="80%" max-width="600px" append-to-body :before-close="cancel">
			<el-form ref="ArchiveStocktakingTaskRef" :model="form" label-width="170px">
				<el-row>
					<!-- <el-col :span="6">
						<el-form-item label="转入供电单位编号" prop="transferOrgNo">
							<el-input v-model="form.transferOrgNo" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col> -->
					<el-col :span="6">
						<el-form-item label="转存任务编号" prop="transferNo">
							<el-input v-model="form.transferNo" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>
					<!-- <el-col :span="6" v-if="form.transferOutRoomNo != null && form.transferOutRoomNo != ''">
						<el-form-item label="转出档案室编号" prop="transferOutRoomNo">
							<el-input v-model="form.transferOutRoomNo" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>

					<el-col :span="6" v-if="form.transferinRoomNo != null && form.transferinRoomNo != ''">
						<el-form-item label="转入档案室编号" prop="transferinRoomNo">
							<el-input v-model="form.transferinRoomNo" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col> -->
					<el-col :span="6">
						<el-form-item label="盒数量" prop="boxNum">
							<el-input v-model="form.boxNum" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="袋数量" prop="bagNum">
							<el-input v-model="form.bagNum" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="档案数量" prop="archNum">
							<el-input v-model="form.archNum" style="width:230px;" disabled="disabled" />
						</el-form-item>
					</el-col>
				</el-row>
				<el-divider content-position="center">档案转存详细</el-divider>

				<el-row style="margin-left: 50px;margin-bottom: 10px;">
					<el-button v-show="form.transferStatus == '04'" type="primary" icon="FolderRemove" @click="rollIn('02')">转入</el-button>
					<el-button v-show="form.transferStatus == '03'" type="success" icon="FolderAdd" @click="rollIn('01')">转出</el-button>
				</el-row>

				<el-row style="min-height: 450px;">
					<el-col :span="24">
						<el-tabs tab-position="left">
							<el-tab-pane label="档案盒">
								<el-table :data="data1" style="border:1px solid #dcdfe6;" :highlight-current-row="true" ref="childData1ref"
								 @selection-change="selecteds1">
									<el-table-column type="selection" width="55" align="center" />
									<el-table-column label="序号" align='center' width="50">
										<template #default="scope">
											{{ scope.$index+1 }}
										</template>
									</el-table-column>
									<el-table-column label="转存状态" align="center" prop="status">
										<template #default="scope">
											<dict-tag :options="rollover_status" :value="scope.row.status" width="100" />
										</template>
									</el-table-column>
									<el-table-column v-if="form.status == '01'"  label="用户编号" prop="custNo" align="center" />
									<el-table-column v-if="form.status == '01'" label="盒条码" prop="boxBarCode" align="center" />
									<el-table-column v-if="form.status == '01'" label="档案盒号" prop="boxNo" align="center" />
									<el-table-column v-if="form.status == '01'" label="库位号" prop="houseLocationNo" align="center" />
									<el-table-column v-if="form.status == '02'" label="转入后库位号" prop="inHouseLocationNo" align="center" />
								</el-table>
								<pagination style="margin-bottom: 20px;" v-show="total1>0" :total="total1" v-model:page="pageNum1"
								 v-model:limit="pageSize1" @pagination="getFileBox" />
							</el-tab-pane>
							<el-tab-pane label="档案袋">
								<el-table :data="data2" style="border:1px solid #dcdfe6;" :highlight-current-row="true" ref="childData2ref"
								 @selection-change="selecteds2">
									<el-table-column type="selection" width="55" align="center" />
									<el-table-column label="序号" align='center' width="50">
										<template #default="scope">
											{{ scope.$index+1 }}
										</template>
									</el-table-column>
									<el-table-column label="转存状态" align="center" prop="status">
										<template #default="scope">
											<dict-tag :options="rollover_status" :value="scope.row.status" width="100" />
										</template>
									</el-table-column>
									<el-table-column label="袋条码" prop="bagCode" align="center" />
									<el-table-column v-if="form.status == '01'" label="档案袋编号" prop="bagNo" align="center" />
									<el-table-column v-if="form.status == '01'" label="档案盒号" prop="boxNo" align="center" />
									<el-table-column v-if="form.status == '01'" label="库位号" prop="houseLocationNo" align="center" />
									<el-table-column v-if="form.status == '02'" label="转入后档案盒号" prop="inBoxNo" align="center" />
									<el-table-column v-if="form.status == '02'" label="转入后库位号" prop="inHouseLocationNo" align="center" />
								</el-table>
								<pagination style="margin-bottom: 20px;" v-show="total2>0" :total="total2" v-model:page="pageNum2"
								 v-model:limit="pageSize2" @pagination="getEnvelopeList" />
							</el-tab-pane>
							<el-tab-pane label="档案">
								<el-table :data="data3" style="border:1px solid #dcdfe6;" :highlight-current-row="true" ref="childData3ref"
								 @selection-change="selecteds3">
									<el-table-column type="selection" width="55" align="center" />
									<el-table-column label="序号" align='center' width="50">
										<template #default="scope">
											{{ scope.$index+1 }}
										</template>
									</el-table-column>
									<el-table-column label="转存状态" align="center" prop="status">
										<template #default="scope">
											<dict-tag :options="rollover_status" :value="scope.row.status" width="100" />
										</template>
									</el-table-column>
									<el-table-column  label="用户编号" prop="custNo" align="center" />
									<el-table-column  label="档案名称" prop="archName" align="center" />
									<el-table-column v-if="form.status == '01'" label="档案盒号" prop="boxNo" align="center" />
									<el-table-column v-if="form.status == '01'" label="库位号" prop="houseLocationNo" align="center" />
									<el-table-column v-if="form.status == '02'" label="转入后档案盒号" prop="inBoxNo" align="center" />
									<el-table-column v-if="form.status == '02'" label="转入后库位号" prop="inHouseLocationNo" align="center" />
								</el-table>
								<pagination style="margin-bottom: 20px;" v-show="total3>0" :total="total3" v-model:page="pageNum3"
								 v-model:limit="pageSize3" @pagination="getArchivesList" />
							</el-tab-pane>
						</el-tabs>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button @click="cancel">关 闭</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="ArchiveUploadTask">
	import {
		listArchiveUploadTask,
		getArchiveUploadTask,
		delArchiveUploadTask,
		addArchiveUploadTask,
		updateArchiveUploadTask,
		getArchiveUploadTaskBoxList,
		getArchiveUploadTaskBagList,
		getArchiveUploadTaskArchiveList,
		reportResults,
		updateArchiveUploadTaskBox
	} from "@/api/archive/ArchiveUploadTask";
	import {
		getCurrentInstance,
		reactive,
		ref,
		toRefs
	} from "vue";
	const {
		proxy
	} = getCurrentInstance();

	const ArchiveUploadTaskList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");

	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			roomNo: null,
			transferOrgNo: null,
			transferNo: null,
			transferOutRoomNo: null,
			transferinRoomNo: null,
			appReason: null,
			status: null,
			transferStatus: null,
			pushStatus: null,
			pushTime: null,
			createTime: null,
		},
		rules: {}
	});

	//档案盒列表
	const childData1 = reactive({
		data1: [],
		selected1: [],
		total1: 1,
		pageNum1: 1,
		pageSize1: 10
	});
	const {
		data1,
		selected1,
		total1,
		pageNum1,
		pageSize1
	} = toRefs(childData1)

	//档案袋
	const childData2 = reactive({
		data2: [],
		selected2: [],
		total2: 1,
		pageNum2: 1,
		pageSize2: 10
	});
	const {
		data2,
		selected2,
		total2,
		pageNum2,
		pageSize2
	} = toRefs(childData2)

	//档案
	const childData3 = reactive({
		data3: [],
		selected3: [],
		total3: 1,
		pageNum3: 1,
		pageSize3: 10
	});
	const {
		data3,
		selected3,
		total3,
		pageNum3,
		pageSize3
	} = toRefs(childData3)

	const {
		queryParams,
		form,
		rules
	} = toRefs(data);
	const {
		push_status,
		execut_result,
		rollover_status,
		upload_in_out_status
	} = proxy.useDict('push_status', 'execut_result', 'rollover_status','upload_in_out_status');


	/** 查询转存任务列表 */
	function getList() {
		loading.value = true;
		listArchiveUploadTask(queryParams.value).then(response => {
			ArchiveUploadTaskList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
		getList()
	}

	// 表单重置
	function reset() {
		form.value = {
			taskId: null,
			orgNo: null,
			roomNo: null,
			transferOrgNo: null,
			transferNo: null,
			transferOutRoomNo: null,
			transferinRoomNo: null,
			boxNum: null,
			bagNum: null,
			archNum: null,
			appReason: null,
			status: "0",
			pushStatus: "0",
			pushTime: null,
			createTime: null,
			creator: null,
			modifyTime: null,
			modifier: null
		};
		proxy.resetForm("ArchiveUploadTaskRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.taskId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 详细按钮操作 */
	function handleUpdate(row) {
		reset();
		const taskId = row.taskId || ids.value
		getArchiveUploadTask(taskId).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "档案转存详细";

			getFileBox();
			getEnvelopeList();
			getArchivesList();
		});
	}

	/**
	 * 获取档案he
	 */
	function getFileBox() {
		getArchiveUploadTaskBoxList({
			taskId: form.value.taskId
		}).then((response) => {
			data1.value = response.rows;
			total1.value = response.total;
		})
	}

	/**
	 * 获取档案袋
	 */
	function getEnvelopeList() {
		getArchiveUploadTaskBagList({
			taskId: form.value.taskId
		}).then((response) => {
			data2.value = response.rows;
			total2.value = response.total;
		})
	}

	/**
	 * 获取档案袋
	 */
	function getArchivesList() {
		getArchiveUploadTaskArchiveList({
			taskId: form.value.taskId
		}).then((response) => {
			data3.value = response.rows;
			total3.value = response.total;
		})
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const taskIds = row.taskId || ids.value;
		proxy.$modal.confirm('是否确认删除转存任务编号为"' + taskIds + '"的数据项？').then(function() {
			return delArchiveUploadTask(taskIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('archive/ArchiveUploadTask/export', {
			...queryParams.value
		}, `入库管理_${new Date().getTime()}.xlsx`)
	}

	/**
	 * 修改状态
	 */
	function revamp(row, status, index) {
		updateArchiveUploadTask({
			taskId: row.taskId,
			status: status
		}).then(response => {
			if (status == 1) {
				proxy.$modal.msgSuccess("出库成功");
			}
			if (status == 2) {
				proxy.$modal.msgSuccess("入库成功");
			}
			getList();
		}).catch(e => {
			proxy.$modal.msgError(e);
		})
	}

	/**
	 * 结果上报
	 */
	function report(row, index, reportType) {
		//根据上报类型判断入库
		if (reportType == '2') {
			if (row.inPushStatus == '01') {
				proxy.$modal.msgWarning("序号" + index + "【已上报】");
				return
			}
		} else {
			if (row.pushStatus == '01') {
				proxy.$modal.msgWarning("序号" + index + "【已上报】");
				return
			}
		}
		reportResults({
			transferNo: row.transferNo,
			pushStatus: '01',
			reportType: reportType
		}).then(response => {
			proxy.$modal.msgSuccess("上报成功");
			getList();
		})
	}

	/**
	 * 选中
	 * @param type
	 */
	function selecteds1(val) {
		console.log(val)
		selected1.value = val
	}

	function selecteds2(val) {
		selected2.value = val
	}

	function selecteds3(val) {
		selected3.value = val
	}

	/**
	 * 转出转入
	 */
	function rollIn(status) {

		let ids = [],
			ids1 = '',
			ids2 = '',
			ids3 = ''
		let msg = ""
		let formData = new FormData();
		let ss = status == "01" ? "03" : "04";
	
		let ssm = ss == "03" ? "待出库" : "待入库"
		selected1.value.forEach(item => {
			if (item.status == ss) {
				ids1 += item.id + ";"
			} else {
				msg += "选中的档案盒存在选中状态为非【" + ssm + "】;"
			}
		})

		selected2.value.forEach(item => {
			if (item.status == ss) {
				ids2 += item.id + ";"
			} else {
				msg += "选中的档案袋存在选中状态为非【" + ssm + "】;"
			}
		})

		selected3.value.forEach(item => {
			if (item.status == ss) {
				ids3 += item.id + ";"
			} else {
				msg += "选中的档案存在选中状态为非【" + ssm + "】;"
			}
		})

		ids.push(ids3)
		ids.push(ids2)
		ids.push(ids1)

		if (msg != '') {
			proxy.$modal.msgError(msg + "请您重新选择！");
			return;
		}

		if (ids1.length == 0 && ids2.length == 0 && ids3.length == 0) {
			if (status == "01") {
				proxy.$modal.msgWarning("请选择需要转出的档案盒或档案袋或档案！");
			} else {
				proxy.$modal.msgWarning("请选择需要转入的档案盒或档案袋或档案！");
			}
			return
		}
		formData.append("ids", ids)
		formData.append("status", status)
		formData.append("taskId", form.value.taskId)


		updateArchiveUploadTaskBox(formData).then(response => {

			if (status == "01") {
				proxy.$modal.msgSuccess("转出档案盒、档案袋、档案成功！");
				form.status = "01"
			} else {
				proxy.$modal.msgSuccess("转入档案盒、档案袋、档案成功！");
			}
			getFileBox()
			getEnvelopeList()
			getArchivesList()
		}).catch(error => {
			//roxy.$modal.msgError(error);
		})
	}

	getList();
</script>
